package com.xiaochong.tir.manager.controller;

import java.util.Date;
import java.util.Map;


import com.xiaochong.service.common.config.redis.RedisServer;
import com.xiaochong.tir.common.data.po.Summary;
import com.xiaochong.tir.common.util.RedisKeysUtils;
import com.xiaochong.tir.manager.service.SummaryService;
import com.xiaochong.tir.manager.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.xc.logclient.utils.LogTrace;
import com.xiaochong.tir.common.data.po.CurrencyIndustryCategory;
import com.xiaochong.tir.manager.enums.StatusEnum;
import com.xiaochong.tir.manager.service.CurrencyIndustryCategoryService;
import com.xiaochong.tir.manager.util.ResultEntity;
import com.xiaochong.tir.manager.util.ReturnResult;

/**   
 * @ClassName:  IndustryController   
 * @Description:TODO(行业管理)   
 * @author: mxh
 * @date:   2018年5月8日 上午11:22:19   
 */

@Controller
@RequestMapping("/manage/industry")
@Api("行业管理")
public class IndustryController {
	
	@Autowired
    private CurrencyIndustryCategoryService industryService;
    @Autowired
    private RedisServer redisServer;
    @Autowired
    private SummaryService summaryService;
    /**   
     * @Title: industryList   
     * @Description: TODO(行业列表页面)   
     * @param: @return      
     * @return: String      
     * @throws   
     */ 
    @RequestMapping(value = "/industryList", method = RequestMethod.GET)
    public String industryList(){
        return "/industry/industryList";
    }

    /**   
     * @Title: childIndustryList   
     * @Description: TODO(子行业列表页面)   
     * @param: @param parentId
     * @param: @param parentName
     * @param: @param map
     * @param: @return      
     * @return: String      
     * @throws   
     */ 
    @RequestMapping(value = "/childIndustryList", method = RequestMethod.GET)
    public String childIndustryList(@RequestParam("parentId") Integer parentId,@RequestParam(value="parentName",required=false) String parentName, Map<String, Object> map){
    	map.put("parentName", parentName);
        map.put("parentId", parentId);
        return "/industry/childIndustryList";
    }

    /**   
     * @Title: addNavigation   
     * @Description: TODO(添加行业页面)   
     * @param: @return      
     * @return: String      
     * @throws   
     */ 
    @RequestMapping(value = "/addIndustry", method = RequestMethod.GET)
    public String addNavigation(){
        return "/industry/addIndustry";
    }

    /**   
     * @Title: addChildIndustry   
     * @Description: TODO(添加子行业页面)   
     * @param: @param parentId
     * @param: @param map
     * @param: @return      
     * @return: String      
     * @throws   
     */ 
    @RequestMapping(value = "/addChildIndustry", method = RequestMethod.GET)
    public String addChildIndustry(@RequestParam("parentId") Integer parentId,@RequestParam(value="parentName",required=false) String parentName, Map<String, Object> map){
    	map.put("parentName", parentName);
        map.put("parentId", parentId);
        return "/industry/addChildIndustry";
    }

    /**   
     * @Title: industryList   
     * @Description: TODO(行业列表)   
     * @param: @param find
     * @param: @param page
     * @param: @param limit
     * @param: @return      
     * @return: Object      
     * @throws   
     */
    @ApiOperation(value = "查询行业列表",notes = "查询行业列表")
    @PostMapping(value = "/industryList")
    @ResponseBody
    public Object industryList(@RequestParam(value = "find", required = false) String find,
    		                  @RequestParam("page") int page,@RequestParam("limit") int limit){
        try {
        	PageInfo<CurrencyIndustryCategory> pageInfo =  industryService.getIndustryByParent(find,page, limit);
        	return new ReturnResult<CurrencyIndustryCategory>(0, "查询成功", pageInfo.getList(), pageInfo.getTotal());
        } catch (Exception e) {
        	e.printStackTrace();
            LogTrace.error(getClass().getSimpleName()+"/industryList", e.getMessage(), e);
        }
        return null;
    }
    
    
    /**   
     * @Title: listChildIndustry   
     * @Description: TODO(子行业列表)   
     * @param: @param find
     * @param: @param parentId
     * @param: @param page
     * @param: @param limit
     * @param: @return      
     * @return: Object      
     * @throws   
     */
    @ApiOperation(value = "查询子行业列表",notes = "查询子行业列表")
    @PostMapping(value = "/listChildIndustry")
    @ResponseBody
    public Object listChildIndustry(@RequestParam(value = "find", required = false) String find,
    		                        @RequestParam("parentId") Integer parentId,
    		                        @RequestParam("page") int page,@RequestParam("limit") int limit){
        try {
        	PageInfo<CurrencyIndustryCategory> pageInfo =  industryService.getIndustryByParentId(find,parentId,page,limit);
        	return new ReturnResult<CurrencyIndustryCategory>(0, "查询成功", pageInfo.getList(), pageInfo.getTotal());
        } catch (Exception e) {
        	e.printStackTrace();
            LogTrace.error(getClass().getSimpleName()+"/listChildIndustry", e.getMessage(), e);
        }
        return null;
    }
    /**   
     * @Title: saveIndustry   
     * @Description: TODO(行业添加)   
     * @param: @param industry
     * @param: @param map
     * @param: @return      
     * @return: String      
     * @throws   
     */
    @ApiOperation(value = "行业添加",notes = "行业添加")
    @RequestMapping(value = "/saveIndustry")
    public String saveIndustry(CurrencyIndustryCategory industry, Map<String, Object> map) {
        redisServer.set(RedisKeysUtils.LOGINNAME,"行业添加");
        try {
            CurrencyIndustryCategory c = industryService.selectByEntity(industry);
            if(null != c){
                return "/industry/industryList";
            }
            //填入行业简介
            Summary summary = new Summary();
            summary.setType("3");
            summary.setRelationId(industry.getId());
            Summary old = summaryService.selectByEntity(summary);
            if(null == old){
                summary.setContent(industry.getContent());
                summary.setCreateTime(new Date());
                summary.setUpdateTime(new Date());
                summaryService.insertSelective(summary);
            }
        	industry.setParentId(null==industry.getParentId()?0:industry.getParentId());
        	industry.setEnableFlag(industry.getStatus().equals(1)?true:false);
        	industry.setDeleteFlag(false);
        	industry.setCreateTime(new Date());
        	industry.setUpdateTime(new Date());
            industryService.insertSelective(industry);
            return "/industry/industryList";
            
        } catch (Exception e) {
            LogTrace.error(getClass().getSimpleName() + "/saveIndustry", e.getMessage(), e);
            return "/industry/saveIndustry";
        }
    }
    /**   
     * @Title: updataIndustry   
     * @Description: TODO(行业编辑)   
     * @param: @param industry
     * @param: @param map
     * @param: @return      
     * @return: String      
     * @throws   
     */
    @ApiOperation(value = "行业编辑",notes = "行业编辑")
    @RequestMapping(value = "/updataIndustry")
    public String updataIndustry(CurrencyIndustryCategory industry, Map<String, Object> map) {
        redisServer.set(RedisKeysUtils.LOGINNAME,"行业编辑");
        try {
            //编辑行业简介
            Summary summary = new Summary();
            summary.setType("3");
            summary.setRelationId(industry.getId());
            Summary old = summaryService.selectByEntity(summary);
            summary.setContent(industry.getContent());
            summary.setCreateTime(new Date());
            summary.setUpdateTime(new Date());
            if(null == old){
                summaryService.insertSelective(summary);
            }else{
                summaryService.updateBySummary(summary);
            }
            CurrencyIndustryCategory industrys = industryService.selectByPrimaryKey(industry.getId());
        	industry.setEnableFlag(industrys.getEnableFlag());
        	industry.setDeleteFlag(false);
        	industry.setUpdateTime(new Date());
            industryService.updateByPrimaryKey(industry,industrys);
            return "/industry/industryList";
        } catch (Exception e) {
            LogTrace.error(getClass().getSimpleName() + "/updataIndustry", e.getMessage(), e);
            return "/industry/updataIndustry";
        }
    }

    
    /**   
     * @Title: updataIndustryStatus   
     * @Description: TODO(更改状态)   
     * @param: @param id
     * @param: @param enableFlag
     * @param: @return      
     * @return: ResultEntity<JSONObject>      
     * @throws   
     */
    @ApiOperation(value = "更改行业状态",notes = "更改行业状态")
    @GetMapping("/updataIndustryStatus")
    @ResponseBody
    public ResultEntity<JSONObject> updataIndustryStatus(@RequestParam("id") Integer id, @RequestParam("enableFlag") String enableFlag){
        redisServer.set(RedisKeysUtils.LOGINNAME,"行业启用/停用");
        ResultEntity<JSONObject> result = new ResultEntity<>();
        try {
        	CurrencyIndustryCategory industry = new CurrencyIndustryCategory();
        	industry.setId(id);
        	industry.setEnableFlag("true".equals(enableFlag)?false:true);
        	industry.setUpdateTime(new Date());
        	industryService.updateByPrimaryKey(industry,industryService.selectByPrimaryKey(id));
            result.setStatus(StatusEnum.OK);
        } catch (Exception e) {
            result.setStatus(StatusEnum.SERVER_ERROR);
            LogTrace.error(getClass().getSimpleName() + "/updataIndustryStatus", e.getMessage(),e);
        }
        return result;
    }


}
